package com.heima.common.Interceptor;

import com.heima.utils.common.ThreadLocalUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/*
 * 拦截器
 * */
@Component
public class UserInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //1.获取请求头中用户id
        String userId = request.getHeader("userId");
        //2.存入ThreadLocal
        if (StringUtils.isNotEmpty(userId)) {
            ThreadLocalUtils.setUserId(Integer.valueOf(userId));
        }
        return true;
    }
}
